<template>
	<view class="add-vehicle-page">
		<!-- 自定义导航栏 -->
		<view class="nav-bar" :style="{ paddingTop: statusBarHeight + 'px' }">
			<view class="nav-content">
				<view class="nav-back" @click="goBack">
					<text class="back-icon">‹</text>
				</view>
				<text class="nav-title">添加车辆</text>
				<view class="nav-right">
					<text class="nav-icon">⋯</text>
					<text class="nav-icon">○</text>
				</view>
			</view>
		</view>

		<!-- 内容区域 -->
		<view class="content-area">
			<!-- 插画区域 -->
			<view class="illustration-area">
				<!-- 车辆插画 -->
				<view class="car-illustration">
					<text class="car-icon">🚗</text>
				</view>
			</view>

			<!-- 表单区域 -->
			<view class="form-area">
				<!-- 车辆类型 -->
				<view class="form-item">
					<text class="form-label">车辆类型</text>
					<picker 
						mode="selector" 
						:range="vehicleTypes" 
						:value="vehicleTypeIndex"
						@change="onVehicleTypeChange"
					>
						<view class="form-input">
							{{ vehicleTypeIndex >= 0 ? vehicleTypes[vehicleTypeIndex] : '请选择' }}
						</view>
					</picker>
				</view>

				<!-- 车辆型号 -->
				<view class="form-item">
					<text class="form-label">车辆型号:</text>
					<picker 
						mode="selector" 
						:range="vehicleModels" 
						:value="vehicleModelIndex"
						@change="onVehicleModelChange"
					>
						<view class="form-input">
							{{ vehicleModelIndex >= 0 ? vehicleModels[vehicleModelIndex] : '请选择' }}
						</view>
					</picker>
				</view>

				<!-- 车牌号码 -->
				<view class="form-item">
					<text class="form-label">车牌号码</text>
					<input 
						class="form-input" 
						v-model="formData.plate" 
						placeholder="请输入您的车牌号" 
						maxlength="10"
					/>
				</view>

				<!-- 车主姓名 -->
				<view class="form-item">
					<text class="form-label">车主姓名</text>
					<input 
						class="form-input" 
						v-model="formData.ownerName" 
						placeholder="请输入您的姓名" 
						maxlength="20"
					/>
				</view>

				<!-- 电话号码 -->
				<view class="form-item">
					<text class="form-label">电话号码</text>
					<input 
						class="form-input" 
						v-model="formData.phone" 
						type="number"
						placeholder="请输入您的手机号" 
						maxlength="11"
					/>
				</view>

				<!-- 照片 -->
				<view class="form-item">
					<text class="form-label">照片</text>
					<view class="photo-upload-area" @click="uploadPhoto">
						<image v-if="formData.photo" :src="formData.photo" class="uploaded-photo" mode="aspectFill" />
						<view v-else class="upload-placeholder">
							<text class="upload-plus">+</text>
							<text class="upload-text">上传车辆照片</text>
						</view>
					</view>
				</view>
			</view>
		</view>

		<!-- 底部添加按钮 -->
		<view class="footer-btn">
			<button class="add-btn" @click="submitVehicle">
				<text class="plus-icon">+</text>
				<text class="add-text">添加车辆</text>
			</button>
		</view>
	</view>
</template>

<script setup>
	import {
		onLoad
	} from '@dcloudio/uni-app';
	import {
		ref
	} from 'vue';

	// 状态栏高度
	const statusBarHeight = ref(0);

	// 表单数据
	const formData = ref({
		vehicleType: '',
		vehicleModel: '',
		plate: '',
		ownerName: '',
		phone: '',
		photo: ''
	});

	// 车辆类型选项
	const vehicleTypes = ref(['轿车', 'SUV', 'MPV', '跑车', '其他']);
	const vehicleTypeIndex = ref(-1);

	// 车辆型号选项
	const vehicleModels = ref(['奔驰-GR', '宝马-X5', '奥迪-A6', '其他']);
	const vehicleModelIndex = ref(-1);

	// 返回上一页
	const goBack = () => {
		uni.navigateBack();
	};

	// 车辆类型选择
	const onVehicleTypeChange = (e) => {
		vehicleTypeIndex.value = e.detail.value;
		formData.value.vehicleType = vehicleTypes.value[e.detail.value];
	};

	// 车辆型号选择
	const onVehicleModelChange = (e) => {
		vehicleModelIndex.value = e.detail.value;
		formData.value.vehicleModel = vehicleModels.value[e.detail.value];
	};

	// 上传照片
	const uploadPhoto = () => {
		uni.chooseImage({
			count: 1,
			sizeType: ['compressed'],
			sourceType: ['album', 'camera'],
			success: (res) => {
				formData.value.photo = res.tempFilePaths[0];
			},
			fail: (err) => {
				console.error('选择图片失败', err);
			}
		});
	};

	// 提交车辆信息
	const submitVehicle = () => {
		// 验证表单
		if (vehicleTypeIndex.value < 0) {
			uni.showToast({
				title: '请选择车辆类型',
				icon: 'none'
			});
			return;
		}

		if (vehicleModelIndex.value < 0) {
			uni.showToast({
				title: '请选择车辆型号',
				icon: 'none'
			});
			return;
		}

		if (!formData.value.plate) {
			uni.showToast({
				title: '请输入车牌号码',
				icon: 'none'
			});
			return;
		}

		if (!formData.value.ownerName) {
			uni.showToast({
				title: '请输入车主姓名',
				icon: 'none'
			});
			return;
		}

		if (!formData.value.phone || formData.value.phone.length !== 11) {
			uni.showToast({
				title: '请输入正确的手机号',
				icon: 'none'
			});
			return;
		}

		// 这里可以调用后端API保存车辆信息
		// 暂时保存到本地存储
		const vehicleList = uni.getStorageSync('vehicleList') || [];
		const newVehicle = {
			model: formData.value.vehicleModel,
			plate: formData.value.plate,
			ownerName: formData.value.ownerName,
			phone: formData.value.phone,
			photo: formData.value.photo,
			isDefault: vehicleList.length === 0 // 第一辆车自动设为默认
		};
		vehicleList.push(newVehicle);
		uni.setStorageSync('vehicleList', vehicleList);

		// 跳转到添加成功页面
		uni.navigateTo({
			url: '/pages/my/AddVehicleSuccess'
		});
	};

	onLoad(() => {
		// 获取状态栏高度
		const systemInfo = uni.getSystemInfoSync();
		statusBarHeight.value = systemInfo.statusBarHeight;
	});
</script>

<style scoped>
	.add-vehicle-page {
		background-color: #f5f5f5;
		min-height: 100vh;
		padding-bottom: 120rpx;
	}

	/* 导航栏 */
	.nav-bar {
		background-color: #007AFF;
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		z-index: 1000;
	}

	.nav-content {
		height: 44px;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 30rpx;
	}

	.nav-back {
		width: 60rpx;
		height: 60rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.back-icon {
		color: #fff;
		font-size: 48rpx;
		font-weight: 300;
		line-height: 1;
	}

	.nav-title {
		color: #fff;
		font-size: 36rpx;
		font-weight: 500;
		flex: 1;
		text-align: center;
	}

	.nav-right {
		display: flex;
		align-items: center;
		gap: 20rpx;
	}

	.nav-icon {
		color: #fff;
		font-size: 40rpx;
	}

	/* 内容区域 */
	.content-area {
		margin-top: calc(44px + var(--status-bar-height, 0px));
		background-color: #fff;
		border-radius: 20rpx 20rpx 0 0;
		padding: 40rpx 30rpx;
	}

	/* 插画区域 */
	.illustration-area {
		height: 300rpx;
		margin-bottom: 40rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.car-illustration {
		width: 100%;
		height: 100%;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.car-icon {
		font-size: 200rpx;
		opacity: 0.6;
	}

	/* 表单区域 */
	.form-area {
		display: flex;
		flex-direction: column;
		gap: 30rpx;
	}

	.form-item {
		display: flex;
		align-items: center;
		gap: 20rpx;
	}

	.form-label {
		width: 160rpx;
		font-size: 32rpx;
		color: #007AFF;
		font-weight: 500;
		flex-shrink: 0;
	}

	.form-input {
		flex: 1;
		height: 80rpx;
		line-height: 80rpx;
		padding: 0 24rpx;
		background-color: #f5f5f5;
		border-radius: 40rpx;
		font-size: 32rpx;
		color: #333;
		box-sizing: border-box;
	}

	/* 照片上传区域 */
	.photo-upload-area {
		flex: 1;
		height: 200rpx;
		background-color: #f5f5f5;
		border-radius: 16rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		overflow: hidden;
	}

	.uploaded-photo {
		width: 100%;
		height: 100%;
	}

	.upload-placeholder {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		gap: 16rpx;
	}

	.upload-plus {
		font-size: 60rpx;
		color: #999;
		line-height: 1;
		font-weight: 300;
	}

	.upload-text {
		font-size: 28rpx;
		color: #999;
	}

	/* 底部添加按钮 */
	.footer-btn {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		padding: 20rpx 30rpx;
		background-color: #fff;
		box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.05);
	}

	.add-btn {
		width: 100%;
		height: 88rpx;
		background-color: #007AFF;
		color: #fff;
		border: none;
		border-radius: 44rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		gap: 10rpx;
		font-size: 32rpx;
		font-weight: 500;
	}

	.plus-icon {
		font-size: 40rpx;
		line-height: 1;
	}

	.add-text {
		font-size: 32rpx;
	}
</style>
